<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <!-- Load ECharts library -->
    <script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.1.0/echarts.min.js"></script>
    <style>
        /* Optional: Some basic styling */
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        #line {
            width: 100%;
            height: 90vh;
        }
    </style>
</head>
<body>

<!-- Chart container -->
<div id="line"></div>

<script>
    // Initialize ECharts instance
    var lineDom = document.getElementById('line');
    var lineChart = echarts.init(lineDom);

    // Option for your chart
    const lineOption = {
        title: {
            text: '超市员工工资前十',
            subtext: 'Data From Mysql',
            left: 'center'
        },
        tooltip: {
            trigger: 'item'
        },
        xAxis: {
            type: 'category',
            data: []
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                data: [],
                type: 'bar',
                itemStyle: {
                    color: 'red'
                }
            }
        ]
    };

    // Fetch data from backend
    fetch("/stats/getmaxsalaryemployee")
        .then(response => response.json())
        .then(res => {
            console.log('Response from backend:', res);
            if (res.code === 1) {
                const name = res.data.map(v => v.name);
                const count = res.data.map(v => v.count);

                console.log('Names:', name);
                console.log('Counts:', count);

                lineOption.xAxis.data = name;
                lineOption.series[0].data = count;

                lineChart.setOption(lineOption);
            } else {
                console.error('Error:', res.msg);
            }
        })
        .catch(error => console.error('Error fetching data:', error));

    // Make the chart responsive
    window.addEventListener('resize', () => {
        lineChart.resize();
    });
</script>

</body>
</html>
